# -*- coding: utf-8 -*-
"""
Created on Tue Sep 24 15:31:33 2013

@author: jkwong
"""
#PBAR_Zspec_DoubleCheckStats.py

# run this script after running PBAR_Zspec_DetermineBestAlgorithm.py

#Check the the binMean values

index = 1
ch = 71

binThreshold = 8
binThresholdCorrected = round(binThreshold / gainCorrection[index,ch])

x = np.arange(256).astype(float)
y = dat[index][:,ch].astype(float)/300./60.
y[0:binThresholdCorrected] = 0.0
binMean = sum(x * y) / y.sum() 
binSTD =  sqrt(sum( ( (binMean - x)**2 ) * y ) / sum(y))

# calculate the corrected values
x = np.arange(256)  * gainCorrection[index,ch]
y = dat[index][:,ch].astype(float)/300./60.
y[0:binThresholdCorrected] = 0.0
binMean_g = sum(x * y) / y.sum() 
binSTD_g =  sqrt(sum( ( (binMean_g - x)**2 ) * y ) / sum(y))

print('binMean')
print("Independently calculated value %3.2f" % binMean)
print("calculate by package value %3.2f" % stats['binMean'][index, ch])

print('binSTD')
print("Independently calculated value %3.2f" % binSTD)
print("calculate by package value %3.2f" % stats['binSTD'][index, ch])

print('binMean_g')
print("Independently calculated value %3.2f" % binMean_g)
print("calculate by package value %3.2f" % stats['binMean_g'][index, ch])

print('binSTD_g')
print("Independently calculated value %3.2f" % binSTD_g)
print("calculate by package value %3.2f" % stats['binSTD_g'][index, ch])

# bin 10
binThresholdCorrected = round(binThreshold / gainCorrection[index,ch])

threshold = 9.0 / gainCorrection[index,ch]
cutt1 = np.arange(256) <= threshold
cutt2 = np.arange(256) > threshold
y = dat[index][:,ch].astype(float)/300./60.
y[0:binThresholdCorrected] = 0
multibin_0_10_g = y[cutt1].sum()
multibin_10_256_g = y[cutt2].sum()

threshold = 9.0
cutt1 = np.arange(256) <= threshold
cutt2 = np.arange(256) > threshold
y = dat[index][:,ch].astype(float)/300./60.
y[0:binThresholdCorrected] = 0
multibin_0_10 = y[cutt1].sum()
multibin_10_256 = y[cutt2].sum()


print('multibin_0_10')
print("Independently calculated value %3.2f" % multibin_0_10)
print("calculate by package value %3.2f" % stats['multibin_0_10'][index, ch])

print('multibin_10_256')
print("Independently calculated value %3.2f" % multibin_10_256)
print("calculate by package value %3.2f" % stats['multibin_10_256'][index, ch])

print('multibin_0_10_g')
print("Independently calculated value %3.2f" % multibin_0_10_g)
print("calculate by package value %3.2f" % stats['multibin_0_10_g'][index, ch])

print('multibin_10_256_g')
print("Independently calculated value %3.2f" % multibin_10_256_g)
print("calculate by package value %3.2f" % stats['multibin_10_256_g'][index, ch])

# check the bin
